﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using ECCIT;
using ECCIT.Web;
using System.IO;
using OgilvyOne.UKNow.Configuration;

public partial class admin_UKNOW_news_new_Phone_Img_add : AdminBasePage
{
    private const string VIEWSTATE_COLUMN_ID = "columnid";
    private const string VIEWSTATE_LAYER_ID = "layerid";
    private const string VIEWSTATE_NewsID = "NewID";
    private const string VIEWSTATE_PHONE_IMG_ID = "PhoneImgID";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //eid = int.Parse(Request.Params["EventsID"]);
            ViewState[VIEWSTATE_COLUMN_ID] = int.Parse(Request.Params[VIEWSTATE_COLUMN_ID]);
            ViewState[VIEWSTATE_LAYER_ID] = Request.Params[VIEWSTATE_LAYER_ID];
            ViewState[VIEWSTATE_NewsID] = Request.Params[VIEWSTATE_NewsID];
            string sPhoneImgID = RequestContext.GetParamValueByParamName(VIEWSTATE_PHONE_IMG_ID, string.Empty);
            ViewState[VIEWSTATE_PHONE_IMG_ID] = sPhoneImgID;

            if (sPhoneImgID != "")
            {
                NewsPhoneImageBLL bll = new NewsPhoneImageBLL(this.LangID);
                EventsImageModel model = bll.selImage(int.Parse(sPhoneImgID));
                BindImageToControl(model);
            }
        }
    }
    private void BindImageToControl(EventsImageModel model)
    {
        if (model == null) return;

        this.ImgName.Text = model.Name;
        this.descTxt.Text = model.Description;
        this.pub.SelectedValue = model.State.ToString();

        this.KeyWord.Text = model.Tag;
        this.Order.Text = model.Order.ToString();//mff
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //string sImgID = (string)ViewState[VIEWSTATE_PHONE_IMG_ID];    // 获取图片ID
        string sImgID = Request.QueryString["PhoneImgID"];

        NewsPhoneImageBLL bll = new NewsPhoneImageBLL(LangID);
        EventsImageModel model = null;

        if (!string.IsNullOrEmpty(sImgID))
            model = bll.selImage(int.Parse(sImgID));           // 如果有图片ID，取回图片model

        string uploadedImageName = null;
        string uploadedThumbImageName = null;


        if (file.PostedFile.ContentLength == 0)
        {
            uploadedImageName = (model == null) ? "" : model.Path;  // 如果用户没有上传图片，且为编辑图片，则图片名称用原来的
        }
        else
        {
            ResultInfo result = UploadedFile.Save(file, ConfigCacheManager.FilePath.UploadImagePath, UploadedFile.RenameHandlers.RenameToGuid);
            uploadedImageName = (result.Result) ? ((FileInfo)result.ReturnObject).Name : null;
        }

        if (file1.PostedFile.ContentLength == 0)
        {
            uploadedThumbImageName = (model == null) ? "" : model.Path1;  // 如果用户没有上传缩略图片，且为编辑图片，则缩略图片名称用原来的
        }
        else
        {
            ResultInfo result = UploadedFile.Save(file1, ConfigCacheManager.FilePath.UploadImagePath, UploadedFile.RenameHandlers.RenameToGuid);
            uploadedThumbImageName = (result.Result) ? ((FileInfo)result.ReturnObject).Name : null;
        }

        if (model == null)
            model = new EventsImageModel();

        //model.EventsID = int.Parse(ViewState[VIEWSTATE_NewsID].ToString());
        model.EventsID = int.Parse(ViewState[VIEWSTATE_NewsID].ToString());
        model.Name = this.ImgName.Text;
        model.Description = this.descTxt.Text;
        model.State = int.Parse(this.pub.SelectedValue);
        model.InsertedTime = DateTime.Now;

        model.Path = uploadedImageName;
        model.Path1 = uploadedThumbImageName;

        model.Tag = this.KeyWord.Text;
        if (this.Order.Text == "" || this.Order.Text == null)//mff
        { model.Order = 0; }
        else
        {
            model.Order = int.Parse(this.Order.Text);//mff
        }
        ResultInfo res = new NewsPhoneImageBLL(LangID).Save(model);    //mff
        if (res.Result)
            Response.Redirect(GetBackUrl());
        else
            Response.Write("<script>alert('保存失败," + res.Message + "')</script>");
    }

    private string GetBackUrl()
    {
        string url = string.Format("news_phone_image_list.aspx?{0}={1}&{2}={3}&{4}={5}&{6}={7}",
            VIEWSTATE_COLUMN_ID, ViewState[VIEWSTATE_COLUMN_ID],
            VIEWSTATE_NewsID, ViewState[VIEWSTATE_NewsID],
            VIEWSTATE_LAYER_ID, ViewState[VIEWSTATE_LAYER_ID],
            "langID", this.LangID);
        return url;
    }
}